<!-- // 商品配对 - 1688采购 -->
<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="产品名称" prop="product_name">
                    <el-input v-model="queryParams.product_name" placeholder="请输入产品名称" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" ></el-input>
                </el-form-item>
                <el-form-item label="seller_sku" prop="seller_sku">
                    <el-input v-model="queryParams.seller_sku" placeholder="请输入商品sku" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" ></el-input>
                </el-form-item>
                <el-form-item label="供应商" prop="supply_id">
                    <SelectPage v-model="queryParams.supply_id" placeholder="请选择供应商" :url="$api.supply.getSupplyList" show_field="supply_name" query_field="supply_name" query_field2="spu" filterable clearable popper-class="custom-select" style="width: 240px" ></SelectPage>
                </el-form-item>
                <el-form-item label="状态" prop="is_pair">
                    <el-select v-model="queryParams.is_pair" placeholder="全部" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in STATUS_LIST" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="更新时间" prop="create_time">
                    <el-date-picker v-model="queryParams.create_time" format="yyyy-MM-dd" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-popover placement="bottom" trigger="hover">
                        <el-button type="primary" slot="reference">添加1688商品</el-button>
                        <div>
                            <div>
                                <el-button type="text" @click="showSingleDddDialog({})">单个添加</el-button>
                            </div>
                            <div style="margin-top: 4px">
                                <el-button type="text" @click="showDetailDialog({})">批量添加</el-button>
                            </div>
                        </div>
                    </el-popover>
                </el-col>

                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="1688商品信息" prop="1688_product_name" min-width="240">
                    <template slot-scope="scope">
                        <div style="display: flex;">
                            <div style="width: 100px;">
                                <ImageUploadImg v-model="scope.row['1688_product_image']" :readOnly="true"></ImageUploadImg>
                            </div>
                            <el-link type="primary" @click="toNewPage(scope.row)">{{ scope.row['1688_product_name'] }}</el-link>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="变种属性" prop="varient_info" min-width="200"></el-table-column>
                <el-table-column label="供应商名称" prop="supply_name" min-width="160"></el-table-column>
                <el-table-column label="商品信息" prop="seller_sku" min-width="240"></el-table-column>
                <el-table-column label="状态" prop="is_pair" min-width="100">
                    <template slot-scope="scope">
                        {{ scope.row.is_pair | formatLabelByValueFuzzy(STATUS_LIST) }}
                    </template>
                </el-table-column>
                <el-table-column label="更新时间" prop="update_time" min-width="160"></el-table-column>
                <el-table-column label="操作" min-width="120" fixed="right">
                    <template slot-scope="scope">
                        <div v-show="scope.row.is_pair == 1"><el-button type="text" icon="el-icon-edit" @click="showSpecifyDialog(scope.row)">更换配对</el-button></div>
                        <div v-show="scope.row.is_pair == 1"><el-button type="text" icon="el-icon-edit" :disabled="loadingDelete" @click="releaseItem(scope.row)">解除配对</el-button></div>
                        <div v-show="scope.row.is_pair == 0"><el-button type="text" icon="el-icon-edit" @click="showSpecifyDialog(scope.row)">手动指定</el-button></div>
                        <div><el-button type="text" icon="el-icon-edit" :disabled="loadingDelete" @click="deleteItem(scope.row)">删除产品</el-button></div>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <single-add-dialog v-model="singleDddDialog.visible" v-if="singleDddDialog.visible" :params="singleDddDialog.data" @success="handleQuery"></single-add-dialog>
        <specify-dialog v-model="specifyDialog.visible" v-if="specifyDialog.visible" :params="specifyDialog.data" @success="handleQuery"></specify-dialog>
    </div>
</template>

<script>
import { getShopList, getSiteList } from '@/api/shop'
import { get_1688_product, delete_1688, release_1688} from '@/api/purchase'
import { parseTime } from '@/utils/quickadmin'
import { STATUS_LIST } from '../constant'
import SingleAddDialog from './components/SingleAddDialog.vue'
import SpecifyDialog from './components/SpecifyDialog.vue'
export default {
    name: 'ChairProductList',
    components: { SingleAddDialog, SpecifyDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            loadingDelete: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 店铺列表下拉
            shopList: [],
            siteList: [],
            STATUS_LIST,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                product_name: undefined,
                seller_sku: undefined,
                supply_id: undefined,
                is_pair: undefined,
                create_time: []
            },
            singleDddDialog: {
                visible: false,
                data: {},
            },
            specifyDialog: {
                visible: false,
                data: {},
            }
        }
    },
    async created() {
        this.loading = true
        try {
            const shopData = await getShopList()
            this.shopList = shopData?.data?.list ?? []
            const siteData = await getSiteList()
            this.siteList = siteData?.data ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        toNewPage(row) {
            window.open(row['1688_product_url'])
        },
        showSingleDddDialog(row) {
            this.singleDddDialog.visible = true
            this.singleDddDialog.data = row
        },
        showSpecifyDialog(row){
            this.specifyDialog.visible = true
            this.specifyDialog.data = row
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        releaseItem(item){
            this.$confirm(`是否确认解除配对?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    this.loadingDelete = true
                    release_1688({id: item.id, old_seller_sku: item.seller_sku})
                        .then(res => {
                            this.$message.success(res.msg)
                            this.resetQuery()
                        })
                        .finally(() => {
                            this.loadingDelete = false
                    })
            }).catch(() => { })
        },
        deleteItem(item){
            this.$confirm(`是否确认删除?`, '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    this.loadingDelete = true
                    delete_1688({id: item.id})
                        .then(res => {
                            this.$message.success(res.msg)
                            this.resetQuery()
                        })
                        .finally(() => {
                            this.loadingDelete = false
                    })
            }).catch(() => { })
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            get_1688_product(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
